﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing.Design;
using System.Windows.Forms.Design;
using System.Windows.Forms;

namespace TA.Win.Utils.Design
{
	/// <summary>
	/// Provides a user interface to edit a complex property.
	/// </summary>
	public class PropertyEditor : UITypeEditor
	{
		/// <summary>
		/// Gets the editor style used by the System.Drawing.Design.UITypeEditor.EditValue(System.IServiceProvider,System.Object) method.
		/// </summary>
		/// <param name="context"></param>
		/// <returns></returns>
		public override UITypeEditorEditStyle GetEditStyle(System.ComponentModel.ITypeDescriptorContext context)
		{
			return UITypeEditorEditStyle.Modal;
		}
		/// <summary>
		/// Edits the value of the specified object using the editor style indicated by the System.Drawing.Design.UITypeEditor.GetEditStyle() method.
		/// </summary>
		/// <param name="context"></param>
		/// <param name="provider"></param>
		/// <param name="value"></param>
		/// <returns></returns>
		public override object EditValue(System.ComponentModel.ITypeDescriptorContext context, IServiceProvider provider, object value)
		{
			if (provider != null)
			{
				IWindowsFormsEditorService editorservice = (IWindowsFormsEditorService)provider.GetService(typeof(IWindowsFormsEditorService));
				if (editorservice != null)
				{
					PropertyDialog dialog = new PropertyDialog();
					dialog.Value = value;
					DialogResult dr = dialog.ShowDialog();
					if (dr == DialogResult.OK)
					{
						context.OnComponentChanged();
						return dialog.Value;
					}
				}
			}

			return value;
		}
	}
}
